package com.bamboowind.config;

import org.springframework.amqp.core.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author cpw
 * @Date 2022/11/2 15:11
 * @Describe: 死信队列消息过期配置类
 */
@Configuration
public class TtlQueueConfig {

    /**
     * 普通交换机X
     */
    public static final String X_EXCHANGE = "X";

    /**
     * 普通队列A
     */
    public static final String QUEUE_A = "QA";

    /**
     * 普通队列B
     */
    public static final String QUEUE_B = "QB";

    /**
     * 死信交换机Y
     */
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";

    /**
     * 死信队列QD
     */
    public static final String DEAD_LETTER_QUEUE = "QD";

    /**
     * 普通队列QC
     */
    public static final String QUEUE_C = "QC";

    /**
     * @return 自定义死信队列失效时间的队列QC
     */
    @Bean("queueC")
    public Queue queueC() {
        Map<String, Object> args = new HashMap<>(2);
        // 声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
    }

    /**
     * @return 队列QC与X绑定
     */
    @Bean
    public Binding queueCBindingX() {
        return BindingBuilder.bind(queueC()).to(xExchange()).with("XC");
    }

    /**
     * @return 普通交换机X
     */
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * @return 死信交换机Y
     */
    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    /**
     * @return 普通队列QA
     */
    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 声明当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 声明当前队列的死信路由key
        arguments.put("x-dead-letter-routing-key", "YD");
        // 声明队列的ttl
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    /**
     * @return 普通队列QB
     */
    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 声明当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 声明当前队列的死信路由key
        arguments.put("x-dead-letter-routing-key", "YD");
        // 声明队列的ttl
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    /**
     * @return 死信队列QD
     */
    @Bean("queueD")
    public Queue queueD() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    /**
     * @return QA与X绑定
     */
    @Bean
    public Binding queueABindingX() {
        return BindingBuilder.bind(queueA()).to(xExchange()).with("XA");
    }

    /**
     * @return QB与X绑定
     */
    @Bean
    public Binding queueBBindingX() {
        return BindingBuilder.bind(queueB()).to(xExchange()).with("XB");
    }

    /**
     * @return 死信队列QD与Y绑定
     */
    @Bean
    public Binding deadLetterBindingQD() {
        return BindingBuilder.bind(queueD()).to(yExchange()).with("YD");
    }
}
